﻿using HotelManager.Common;
using HotelManager.Extensions;
using HotelManager.Models;
using HotelManager.Service;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Shapes;

namespace HotelManager.ViewModels
{
    public class RoleViewModel: NavigationViewModel
    {
        private readonly IDialogHostService dialog;
        private readonly IRoleService roleService;
        public RoleViewModel(IRoleService _roleService, IContainerProvider provider) : base(provider)
        {
            RoleLists = new ObservableCollection<RoleData>();
            this.dialog = provider.Resolve<IDialogHostService>();
            roleService= _roleService;
            ExecuteCommand = new DelegateCommand<string>(Execute);
            SelectedCommand = new DelegateCommand<RoleData>(Selected);
            DelCommand = new DelegateCommand<RoleData>(Delete);
        }
        private async void Delete(RoleData obj)
        {
            var dialogResult = await dialog.Question("温馨提示", $"确认删除角色：{obj.RoleName}?");
            if (dialogResult.Result != ButtonResult.OK) return;
            var deleteResult = await roleService.DeleteAsync(obj.RoleId);
            if (deleteResult.success)
            {
                var model = RoleLists.FirstOrDefault(o => o.RoleId.Equals(obj.RoleId));
                if (model != null)
                    RoleLists.Remove(model);
            }
        }
        private void Execute(string Operater)
        {
            switch(Operater)
            {
                case "Add":
                    OpenDraw();
                    break;
                case "Save":
                    Save();
                    break;
            }
        }
        private async void Selected(RoleData roleData)
        {
            try
            {
                UpdateLoading(true);
                var memoResult = await roleService.GetFirstOrDefaultAsync(roleData.RoleId);
                if (memoResult is not null)
                {
                    CurrentDto = memoResult;
                    IsRightDrawerOpen = true;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                UpdateLoading(false);
            }
        }
        private async void Save()
        {
            if (string.IsNullOrWhiteSpace(CurrentDto.RoleName))
                return;
            UpdateLoading(true);
            try
            {
                if (CurrentDto.RoleId > 0)
                {
                    var updateResult = roleService.Update(CurrentDto);
                    if (updateResult.success)
                    {
                        var todo = RoleLists.FirstOrDefault(t => t.RoleId == CurrentDto.RoleId);
                        if (todo != null)
                        {
                            todo.RoleName = CurrentDto.RoleName;
                            GetDataAsync();
                        }
                    }
                    IsRightDrawerOpen = false;
                }
                else
                {
                    var addResult = await roleService.AddAsync(CurrentDto.RoleName);
                    if (addResult.success)
                    {
                        //RoleLists.Add(addResult.Result);
                        GetDataAsync();
                        IsRightDrawerOpen = false;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            finally { UpdateLoading(false); }
        }
        private bool isRightDrawerOpen;

        public bool IsRightDrawerOpen
        {
            get { return isRightDrawerOpen; }
            set { isRightDrawerOpen = value; RaisePropertyChanged(); }
        }
        private RoleData currentDto;

        public RoleData CurrentDto
        {
            get { return currentDto; }
            set { currentDto = value; RaisePropertyChanged(); }
        }
        private void OpenDraw()
        {
            CurrentDto=new RoleData();
            IsRightDrawerOpen = true;

        }
        public ObservableCollection<RoleData> roleLists;
        public ObservableCollection<RoleData> RoleLists
        {
            get { return roleLists; }
            set { roleLists = value; RaisePropertyChanged(); }
        }
        public DelegateCommand<string> ExecuteCommand { get; private set; }
        public DelegateCommand<RoleData> SelectedCommand { get; private set; }
        public DelegateCommand<RoleData> DelCommand { get; private set; }
        async void GetDataAsync()
        {
            UpdateLoading(true);

            var memoResult = await roleService.GetAllAsync();
            if (memoResult.Any())
            {
                RoleLists.Clear();
                foreach (var item in memoResult)
                {
                    RoleLists.Add(item);
                }
            }
            UpdateLoading(false);
        }
        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);
            GetDataAsync();
        }
    }
}
